package com.fruits.login.controller;


import com.fruits.login.annotation.EnCrypt;
import com.fruits.login.model.HallUrl;
import com.fruits.login.model.PlayerLoginInfo;
import com.fruits.login.model.Result;
import com.fruits.login.service.HallService;
import com.fruits.util.IPUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping
public class HallController {
    private static final Logger logger = LoggerFactory.getLogger(HallController.class);

    @Autowired
    private HallService hallService;

    /**
     * 大厅校验验证码
     * @return
     */
    @PostMapping("/checkCode")
    @EnCrypt(ignore = true)
    public Result<String> checkCode(@RequestBody PlayerLoginInfo hall, HttpServletRequest request){
        String ip = IPUtil.getIPAddress(request);
        logger.info(ip+ "大厅校验验证码");
        if (hall.getUid() == null || hall.getPhone() == null || hall.getCode() == null){
            return Result.valueOf(-1,"请求参数错误");
        }
        if (!hallService.ipWhiteList(ip)){
            return Result.valueOf(-1,"非白名单IP");
        }
        return hallService.checkCode(hall);
    }

    @PostMapping("getHallOpenUrl")
    @EnCrypt(ignore = true)
    public List<HallUrl> listOpenHall(){
        List<HallUrl> hallUrls = hallService.listOpenHall();
        hallUrls.forEach(hallUrl -> log.info(hallUrl.toString()));
        return hallUrls;
    }
}
